package com.example.myapplication.found.model;

import com.example.myapplication.found.IndustryFound;
import com.example.myapplication.found.bean.StockBean;
import com.example.myapplication.found.bean.StockKlineBean;
import com.example.myapplication.found.bean.StockOrderBean;
import com.example.myapplication.found.contract.PointCallback;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author: rongfeng
 * @date: 2022/2/25
 * @description
 */
public class ChengJiaoLiang {

    private String fundCode;
    private String fundName;
    private StockBean stockBean;
    private StockBean weekStockBean;
    private ExecutorService pool;
    PointCallback simpleCallback;
    List<StockOrderBean> orderList = new ArrayList<>();

    public void setSimpleCallback(PointCallback simpleCallback) {
        this.simpleCallback = simpleCallback;
    }

    public ChengJiaoLiang(StockBean stockBean, StockBean weekStockBean) {
        this.fundCode = stockBean.code;
        this.fundName = stockBean.name;
        this.stockBean = stockBean;
        this.weekStockBean = weekStockBean;
        init();
    }

    public void init() {
        pool = Executors.newFixedThreadPool(1);
    }


    public void start() {
        int lastDealIndex = 0;
        for (int i = 1; i < stockBean.klineList.size(); i++) {
            StockKlineBean klineBean = stockBean.klineList.get(i);
            StockKlineBean lastKlineBean = stockBean.klineList.get(i - 1);


            if (klineBean.index > lastDealIndex
                    && klineBean.shouPan > lastKlineBean.jinKai
                    && klineBean.chg > 0
                    && klineBean.chg < 4) {

                float money = 10000;
                for (int j = 0; j < 2; j++) {
                    if (j + i + 1 < stockBean.klineList.size()) {
                        StockKlineBean nextBean = stockBean.klineList.get(j + i + 1);
                        money = money + (money * (nextBean.chg / 100));
                        lastDealIndex = j + i + 1;
                        if (IndustryFound.isTest) {
                            System.out.println(nextBean.date + " 涨跌 " + nextBean.chg);
                            System.out.println("money " + money);
                            System.out.println();
                        }
                    }
                }

                StockOrderBean orderBean = new StockOrderBean();
                orderBean.average = money;
                orderBean.zhang = money > 10000;
                orderBean.die = money < 10000;
                orderBean.pin = money == 10000;
                orderBean.money = money;
                orderList.add(orderBean);
            }
        }
        float orderZhang = 0, orderDie = 0, orderPin = 0, orderAvg = 0, money = 0;
        for (StockOrderBean orderBean : orderList) {
            if (orderBean.zhang) {
                orderZhang++;
            }
            if (orderBean.die) {
                orderDie++;
            }
            if (orderBean.pin) {
                orderPin++;
            }
            money = money + orderBean.money;
            orderAvg = orderAvg + orderBean.average;
        }
        if (orderList.size() > 0) {
            System.out.println(fundName + " " + fundCode);
            System.out.println("买入涨 " + orderZhang + " 跌 " + orderDie + "  平 " + orderPin + " 胜率 " + (orderZhang / orderList.size()) * 100 + "%");
            System.out.println("平均剩余金额 " + money / orderList.size());
            System.out.println();
        }
        if (simpleCallback != null) {
            simpleCallback.pointCallback(orderList);
        }
    }

}
